package yyds.sniarbtej.transformer.intellij;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import yyds.sniarbtej.transformer.BaseClassTransformer;
import yyds.sniarbtej.transformer.ClassRewriter;

public final class BlockValidationHost extends BaseClassTransformer {
    public String canTransformClassName() {
        return "sun/net/www/http/HttpClient";
    }

    public byte[] rewrite(byte[] in) {
        return transform(in, new ClassRewriter() {
            @Override
            public void rewriteMethod(MethodNode node) {
                if ("openServer".equals(node.name) && "()V".equals(node.desc)) {
                    InsnList insn = new InsnList();
                    insn.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    insn.add(new FieldInsnNode(Opcodes.GETFIELD, "sun/net/www/http/HttpClient", "url", "Ljava/net/URL;"));
                    insn.add(new MethodInsnNode(Opcodes.INVOKESTATIC, methodOverride, "C1", "(Ljava/net/URL;)Ljava/net/URL;", false));
                    insn.add(new InsnNode(Opcodes.POP));
                    node.instructions.insert(insn);
                }
            }
        });
    }
}
